package com.wwfish.cmsui.modules.common.client;

import com.nexustar.gwt.dashboard.client.AbstractPage;
import com.nexustar.gwt.widgets.client.ui.table.ViewTable;
import com.wwfish.gwt.reflection.client.ModelReflection;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Tom
 * Date: 2010-7-31
 * Time: 17:03:53
 * To change this template use File | Settings | File Templates.
 */
public abstract class BaseCMSPage extends AbstractPage {

    public void addRow(ViewTable table, Object userObject) {
        List list = (List) table.getModelManger().getModel();
        if (list == null) {
            list = new ArrayList();
            table.getModelManger().cacheModel(list);
        }
        list.add(0, userObject);
        table.getModelManger().refresh();
    }

    public void updateRow(ViewTable table, Object userObject) {
        List list = (List) table.getModelManger().getModel();
        if (list == null) {
            list = new ArrayList();
            table.getModelManger().cacheModel(list);
        }
        int position = list.indexOf(userObject);
        if (position != -1) {
            ModelReflection.copyProperties(userObject, list.get(position));
            table.getModelManger().refresh();
        } else
            addRow(table, userObject);
    }

    public void deleteRow(ViewTable table, Object userObject) {
        List list = (List) table.getModelManger().getModel();
        list.remove(userObject);
        table.getModelManger().refresh();

    }
}
